package com.engine.jucailinkq.attendance.workflow.cmd;

import com.api.browser.bean.SearchConditionItem;
import com.api.hrm.bean.WeaRadioGroup;
import com.api.hrm.util.HrmAdvancedSearchUtil;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.common.service.HrmCommonService;
import com.engine.common.service.impl.HrmCommonServiceImpl;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Lists;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.settings.ChgPasswdReminder;
import weaver.hrm.settings.RemindSettings;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: sy
 * @Description: 出勤汇总查询条件
 * @Date: 2024/3/21
 **/
public class GetCqSummarySearchConditionCmd extends AbstractCommonCommand<Map<String, Object>> {

    public GetCqSummarySearchConditionCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    BaseBean bs = new BaseBean();

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {Map<String,Object> retmap = new HashMap<String,Object>();
        List<Object> lsCondition = new ArrayList<Object>();
        List<SearchConditionItem> cusCondition = new ArrayList<SearchConditionItem>();
        String[] options = null;
        String[] selectLinkageDatas = null;
        try{
            HrmAdvancedSearchUtil hrmAdvancedSearchUtil = new HrmAdvancedSearchUtil();
            HrmCommonService hrmCommonService = new HrmCommonServiceImpl();

//            boolean kq_personal_reportsearch = KQSettingsBiz.showLeaveTypeSet("kq_personal_reportsearch");
            boolean kq_personal_reportsearch = false;
            //展示列、时间范围、数据范围
            WeaRadioGroup wrg = null;
            if(kq_personal_reportsearch) {
                options = new String[]{"1,15537,false","2,15539,false","3,15541,false","7,27347,false","4,21904,false","6,32530,false", "9,18196,true", "10,547111,false"};
            } else {
                options = new String[]{"1,15537,false","2,15539,false","3,15541,false","7,27347,false","4,21904,false","5,15384,false","8,81716,false","6,32530,false", "9,18196,true", "10,547111,false"};
            }
            wrg = hrmAdvancedSearchUtil.getAdvanceCondition("typeselect","19482",options,null,user);
            wrg.setLabelcol(3);
            wrg.setFieldcol(20);
            Map<String,Object> selectLinks = new HashMap<String, Object>();
            List<String>  domkey =  new  ArrayList<String>();
            Map<String,Object> map = new HashMap<String, Object>();
            selectLinks.put("conditionType", "RANGEPICKER");
            domkey = new  ArrayList<String>();
            domkey.add("fromDate");
            domkey.add("toDate");
            selectLinks.put("domkey", domkey);
            map.put("6", selectLinks);
            wrg.setSelectLinkageDatas(map);
            bs.writeLog("wrg_options : " + wrg.getOptions());
            lsCondition.add(wrg);

            if(kq_personal_reportsearch) {
                if(hrmCommonService.isManager(user.getUID())){
                    options = new String[]{"3,1867,false","4,15089,false"};
                } else {
                    options = new String[]{"3,1867,false"};
                }
            } else {
                if(hrmCommonService.isManager(user.getUID())){
                    options = new String[]{"0,140,true","1,141,false","2,124,false","3,1867,false","4,15089,false"};
                }else{
                    options = new String[]{"0,140,true","1,141,false","2,124,false","3,1867,false"};
                }
            }

            selectLinkageDatas = new String[]{"1,subCompanyId,141,3,194","2,departmentId,124,3,57","3,resourceId,1867,3,17","4,allLevel,389995,4,1"};
            if(user.getUID()!=1){
                if(kq_personal_reportsearch) {
                    selectLinkageDatas[2]="3,resourceId,1867,3,17,"+user.getUID();
                    options[0]="3,1867,true";
                } else {
                    selectLinkageDatas[2]="3,resourceId,1867,3,17,"+user.getUID();
                    options[0]="0,140,false";
                    options[3]="3,1867,true";
                }
            }
            wrg = hrmAdvancedSearchUtil.getAdvanceCondition("viewScope","34102",options,selectLinkageDatas,user);
            wrg.setLabelcol(3);
            wrg.setFieldcol(20);
            lsCondition.add(wrg);

            WeaRadioGroup wrg1 = new WeaRadioGroup();

            ChgPasswdReminder reminder = new ChgPasswdReminder();
            RemindSettings settings = reminder.getRemindSettings();
            String checkUnJob = Util.null2String(settings.getCheckUnJob(), "0");
            List<String> statusList = Lists.newArrayList();
            if ("1".equals(checkUnJob)) {//启用后，只有有“离职人员查看”权限的用户才能检索非在职人员
                if (HrmUserVarify.checkUserRight("hrm:departureView", user)) {
                    statusList.add("9,332,false");
                }
            } else {
                statusList.add("9,332,false");
            }
            statusList.add("0,15710,false");
            statusList.add("1,15711,false");
            statusList.add("2,480,false");
            statusList.add("3,15844,false");
            if ("1".equals(checkUnJob)) {//启用后，只有有“离职人员查看”权限的用户才能检索非在职人员
                if (HrmUserVarify.checkUserRight("hrm:departureView", user)) {
                    statusList.add("4,6094,false");
                    statusList.add("5,6091,false");
                    statusList.add("6,6092,false");
                    statusList.add("7,2245,false");
                }
            } else {
                statusList.add("4,6094,false");
                statusList.add("5,6091,false");
                statusList.add("6,6092,false");
                statusList.add("7,2245,false");
            }
            statusList.add("8,1831,true");
            options = new String[statusList.size()];
            for(int i = 0 ; i < statusList.size() ; i++){
                String statusStr = statusList.get(i);
                options[i] = statusStr;
            }

            wrg1 = hrmAdvancedSearchUtil.getAdvanceCondition("status","602",options,null,user);
            wrg1.setLabelcol(3);
            wrg1.setFieldcol(20);
            lsCondition.add(wrg1);

            List<Object> showColumns = new ArrayList<>();
            String cascadekey = "";
            List<Map<String,Object>> selectOptions = new ArrayList<>();
            Map<String,Object> selectOption = null;

            //设置展示列
            //个人信息相关
            selectOption = new HashMap<>();
            selectOption.put("key","subcompany");
            selectOption.put("cascadekey",cascadekey);
            selectOption.put("showname",SystemEnv.getHtmlLabelName(528842, user.getLanguage()));//分部
            selectOption.put("selected",true);
            selectOptions.add(selectOption);

            selectOption = new HashMap<>();
            selectOption.put("key","department");
            selectOption.put("cascadekey",cascadekey);
            selectOption.put("showname",SystemEnv.getHtmlLabelName(528843, user.getLanguage()));//部门
            selectOption.put("selected",true);
            selectOptions.add(selectOption);

            showColumns.add(selectOptions);
            //考勤项目
            String kqxmSql = "select * from uf_jcl_kq_kqxm";
            List<Map<String,Object>> kqxmDates = DbTools.getSqlToList(kqxmSql);
            bs.writeLog("kqxmDates : " + kqxmDates);
            selectOptions = new ArrayList<>();
            for(Map<String,Object> kqxm : kqxmDates) {
                selectOption = new HashMap<>();
                selectOption.put("key", "kqxm" + "_" + kqxm.get("id"));
                selectOption.put("cascadekey",cascadekey);
                selectOption.put("showname",kqxm.get("mc"));
                selectOption.put("selected",false);
                selectOptions.add(selectOption);
            }
            showColumns.add(selectOptions);

            retmap.put("showColumns", showColumns);
            createSearchConditionItemList(cusCondition);

            retmap.put("status", "1");
            retmap.put("conditions", lsCondition);
            retmap.put("cusCondition", cusCondition);
        }catch (Exception e) {
            writeLog(e);
            retmap.put("status", "-1");
            retmap.put("message", ""+ SystemEnv.getHtmlLabelName(10004510,weaver.general.ThreadVarLanguage.getLang())+"");
        }
        return retmap;
    }

    /**
     * 这里只处理非下拉框字段
     * @param cusCondition
     */
    public void createSearchConditionItemList(List<SearchConditionItem> cusCondition) {

    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

}
